package com.accloud.cloudservice;

import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import com.accloud.common.ACConfiguration;
import com.accloud.service.ACDeviceDataMgr;
import com.accloud.service.ACException;
import com.accloud.service.ACMsg;
import com.accloud.service.DeviceTopic;
import com.accloud.service.QueryOption;
import com.accloud.service.Receiver;
import com.accloud.service.Topic;
import com.accloud.service.TopicData;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/* access modifiers changed from: package-private */
public class ACDeviceDataManager extends ACAbsDataManager implements ACDeviceDataMgr, Receiver<TopicData> {
    static final int FAST_REPORT_DURATION = 180;
    private final String SERVICE = "zc-dstore";
    private final int SERVICE_VERSION = 1;
    private Handler mHandler;
    private HashSet<NotifyFastReportTask> mNotifyTasks = new HashSet<>();
    private ACDeviceDataMgr.OnlineStatusListener mOnlineStatusListener;
    private ACDeviceDataMgr.PropertyReceiver mPropertyReceiver;
    private ACDeviceDataMgr.WarningReceiver mWarningReceiver;

    ACDeviceDataManager() {
        AC.sSockectManager.setDeviceDataReceiver(this);
    }

    @Override // com.accloud.service.ACDeviceDataMgr
    public void enableDeviceFastReport(String subDomain, long deviceId, int interval, VoidCallback callback) {
        if (this.mHandler == null) {
            this.mHandler = new Handler(Looper.getMainLooper());
        }
        NotifyFastReportTask task = new NotifyFastReportTask(subDomain, deviceId, interval, callback);
        this.mNotifyTasks.add(task);
        this.mHandler.post(task);
    }

    @Override // com.accloud.service.ACDeviceDataMgr
    public void disableDeviceFastReport(String subDomain, long deviceId, VoidCallback callback) {
        if (this.mHandler != null) {
            NotifyFastReportTask toBeRemoved = null;
            Iterator<NotifyFastReportTask> it = this.mNotifyTasks.iterator();
            while (true) {
                if (!it.hasNext()) {
                    break;
                }
                NotifyFastReportTask task = it.next();
                if (task.subDomain.equals(subDomain) && task.deviceId == deviceId) {
                    toBeRemoved = task;
                    this.mHandler.removeCallbacks(task);
                    break;
                }
            }
            if (toBeRemoved != null) {
                this.mNotifyTasks.remove(toBeRemoved);
            }
            callback.success();
        }
    }

    @Override // com.accloud.service.ACDeviceDataMgr
    public void disableAllDeviceFastReport() {
        if (this.mHandler != null) {
            Iterator<NotifyFastReportTask> it = this.mNotifyTasks.iterator();
            while (it.hasNext()) {
                this.mHandler.removeCallbacks(it.next());
            }
            this.mNotifyTasks.clear();
        }
    }

    @Override // com.accloud.service.ACDeviceDataMgr
    public void subscribeProperty(String subDomain, long deviceId, VoidCallback callback) {
        subscribe(DeviceTopic.property(subDomain, deviceId), callback);
    }

    @Override // com.accloud.service.ACDeviceDataMgr
    public void unSubscribeProperty(String subDomain, long deviceId, VoidCallback callback) {
        unSubscribe(DeviceTopic.property(subDomain, deviceId), callback);
    }

    @Override // com.accloud.service.ACDeviceDataMgr
    public void unSubscribeAllProperty() {
        unSubscribeAllType(Topic.PROPERTY);
    }

    @Override // com.accloud.service.ACDeviceDataMgr
    public void registerPropertyReceiver(ACDeviceDataMgr.PropertyReceiver receiver) {
        this.mPropertyReceiver = receiver;
    }

    @Override // com.accloud.service.ACDeviceDataMgr
    public void unregisterPropertyReceiver(ACDeviceDataMgr.PropertyReceiver receiver) {
        this.mPropertyReceiver = null;
    }

    @Override // com.accloud.service.ACDeviceDataMgr
    public void subscribeOnlineStatus(String subDomain, long deviceId, VoidCallback callback) {
        subscribe(DeviceTopic.status(subDomain, deviceId), callback);
    }

    @Override // com.accloud.service.ACDeviceDataMgr
    public void unSubscribeOnlineStatus(String subDomain, long deviceId, VoidCallback callback) {
        unSubscribe(DeviceTopic.status(subDomain, deviceId), callback);
    }

    @Override // com.accloud.service.ACDeviceDataMgr
    public void unSubscribeAllOnlineStatus() {
        unSubscribeAllType(Topic.STATUS);
    }

    @Override // com.accloud.service.ACDeviceDataMgr
    public void registerOnlineStatusListener(ACDeviceDataMgr.OnlineStatusListener listener) {
        this.mOnlineStatusListener = listener;
    }

    @Override // com.accloud.service.ACDeviceDataMgr
    public void unregisterOnlineStatusListener(ACDeviceDataMgr.OnlineStatusListener listener) {
        this.mOnlineStatusListener = null;
    }

    @Override // com.accloud.service.ACDeviceDataMgr
    public void subscribeWarning(String subDomain, long deviceId, VoidCallback callback) {
        subscribe(DeviceTopic.warning(subDomain, deviceId), callback);
    }

    @Override // com.accloud.service.ACDeviceDataMgr
    public void unSubscribeWarning(String subDomain, long deviceId, VoidCallback callback) {
        unSubscribe(DeviceTopic.warning(subDomain, deviceId), callback);
    }

    @Override // com.accloud.service.ACDeviceDataMgr
    public void unSubscribeAllWarning() {
        unSubscribeAllType(Topic.WARNING);
    }

    @Override // com.accloud.service.ACDeviceDataMgr
    public void registerWarningReceiver(ACDeviceDataMgr.WarningReceiver receiver) {
        this.mWarningReceiver = receiver;
    }

    @Override // com.accloud.service.ACDeviceDataMgr
    public void unregisterWarningReceiver(ACDeviceDataMgr.WarningReceiver receiver) {
        this.mWarningReceiver = null;
    }

    @Override // com.accloud.service.ACDeviceDataMgr
    public void fetchHistoryProperty(String subDomain, long deviceId, QueryOption queryOption, final PayloadCallback<List<String>> callback) {
        if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
            return;
        }
        ScanQueryParameter queryParameter = new ScanQueryParameter(deviceId, queryOption);
        queryParameter.setContext(subDomain);
        queryParameter.setName("scanDeviceData");
        new ACServiceClient("zc-dstore", 1, ACConfiguration.getRouterAddr(), queryParameter, callback, new ACMsgListener() {
            /* class com.accloud.cloudservice.ACDeviceDataManager.AnonymousClass1 */

            @Override // com.accloud.cloudservice.ACMsgListener
            public void finish(ACMsg resp) {
                HistoryPropertyResult historyPropertyResult = (HistoryPropertyResult) new Gson().fromJson(new String(resp.getPayload()), HistoryPropertyResult.class);
                if (historyPropertyResult == null || historyPropertyResult.objects == null) {
                    callback.success(null);
                    return;
                }
                ArrayList arrayList = new ArrayList();
                for (JsonElement json : historyPropertyResult.objects) {
                    arrayList.add(json.toString());
                }
                callback.success(arrayList);
            }
        }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Void[0]);
    }

    @Override // com.accloud.service.ACDeviceDataMgr
    public void fetchCurrentProperty(String subDomain, long deviceId, final PayloadCallback<String> callback) {
        if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
            return;
        }
        FindQueryParameter queryParameter = new FindQueryParameter(deviceId);
        queryParameter.setContext(subDomain);
        queryParameter.setName("findDeviceData");
        new ACServiceClient("zc-dstore", 1, ACConfiguration.getRouterAddr(), queryParameter, callback, new ACMsgListener() {
            /* class com.accloud.cloudservice.ACDeviceDataManager.AnonymousClass2 */

            @Override // com.accloud.cloudservice.ACMsgListener
            public void finish(ACMsg resp) {
                CurrentPropertyResult currentPropertyResult = (CurrentPropertyResult) new Gson().fromJson(new String(resp.getPayload()), CurrentPropertyResult.class);
                if (currentPropertyResult != null && currentPropertyResult.object != null) {
                    callback.success(currentPropertyResult.object.toString());
                }
            }
        }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Void[0]);
    }

    @Override // com.accloud.service.ACDeviceDataMgr
    public void fetchWarningStatus(String subDomain, long deviceId, final PayloadCallback<String> callback) {
        ACMsg msg = ACMsg.getACMsg(subDomain);
        msg.setName("getDeviceFaultStatus");
        msg.put("logicalId", Long.valueOf(deviceId));
        new HashMap();
        new ACServiceClient("zc-device-service", 1, ACConfiguration.getRouterAddr(), msg, callback, new ACMsgListener() {
            /* class com.accloud.cloudservice.ACDeviceDataManager.AnonymousClass3 */

            @Override // com.accloud.cloudservice.ACMsgListener
            public void finish(ACMsg resp) {
                JsonArray faultStatus = ((CurrentWarningResult) new Gson().fromJson(new String(resp.getPayload()), CurrentWarningResult.class)).faultStatus;
                JsonObject result = new JsonObject();
                Iterator<JsonElement> it = faultStatus.iterator();
                while (it.hasNext()) {
                    JsonObject item = it.next().getAsJsonObject();
                    result.add(item.get("category").getAsString(), item.get("value"));
                }
                callback.success(new Gson().toJson((JsonElement) result));
            }
        }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Void[0]);
    }

    public void onReceive(TopicData deviceData) {
        String subDomain = deviceData.getSubDomain();
        Long deviceId = Long.valueOf(deviceData.getKey());
        String value = deviceData.getValue();
        String type = deviceData.getType();
        char c = 65535;
        switch (type.hashCode()) {
            case -336102634:
                if (type.equals(Topic.STATUS)) {
                    c = 1;
                    break;
                }
                break;
            case 1111052281:
                if (type.equals(Topic.PROPERTY)) {
                    c = 0;
                    break;
                }
                break;
            case 1192323320:
                if (type.equals(Topic.WARNING)) {
                    c = 2;
                    break;
                }
                break;
        }
        switch (c) {
            case 0:
                if (this.mPropertyReceiver != null) {
                    this.mPropertyReceiver.onPropertyReceive(subDomain, deviceId.longValue(), value);
                    return;
                }
                return;
            case 1:
                if (this.mOnlineStatusListener != null) {
                    JsonElement jsonElement = (JsonElement) new Gson().fromJson(value,  JsonElement.class);
                    if (jsonElement.isJsonObject() && jsonElement.getAsJsonObject().has("online")) {
                        this.mOnlineStatusListener.onStatusChanged(subDomain, deviceId.longValue(), jsonElement.getAsJsonObject().get("online").getAsBoolean());
                        return;
                    }
                    return;
                }
                return;
            case 2:
                if (this.mWarningReceiver != null) {
                    Gson gson = new Gson();
                    JsonObject transformed = new JsonObject();
                    for (Map.Entry<String, JsonElement> entry : ((JsonObject) gson.fromJson(value,  JsonObject.class)).entrySet()) {
                        transformed.add(entry.getKey().replace("_faults_", ""), entry.getValue());
                    }
                    this.mWarningReceiver.onWarningReceive(subDomain, deviceId.longValue(), gson.toJson((JsonElement) transformed));
                    return;
                }
                return;
            default:
                return;
        }
    }

    private static class HistoryPropertyResult {
        JsonElement[] objects;

        private HistoryPropertyResult() {
        }
    }

    private static class CurrentPropertyResult {
        JsonElement object;

        private CurrentPropertyResult() {
        }
    }

    private static class CurrentWarningResult {
        JsonArray faultStatus;

        private CurrentWarningResult() {
        }
    }

    private class NotifyFastReportTask implements Runnable {
        private VoidCallback callback;
        private final VoidCallback callbackDelegate = new VoidCallback() {
            /* class com.accloud.cloudservice.ACDeviceDataManager.NotifyFastReportTask.AnonymousClass1 */

            @Override // com.accloud.cloudservice.VoidCallback
            public void success() {
                if (NotifyFastReportTask.this.callback != null) {
                    NotifyFastReportTask.this.callback.success();
                }
            }

            @Override // com.accloud.cloudservice.BaseCallback
            public void error(ACException e) {
                if (NotifyFastReportTask.this.callback != null) {
                    NotifyFastReportTask.this.callback.error(e);
                }
            }
        };
        private final long deviceId;
        private final int interval;
        private final String subDomain;

        public NotifyFastReportTask(String subDomain2, long deviceId2, int interval2, VoidCallback callback2) {
            this.subDomain = subDomain2;
            this.deviceId = deviceId2;
            this.interval = interval2;
            this.callback = callback2;
        }

        @Override // java.lang.Runnable
        public void run() {
            ACMsg msg = ACMsg.getACMsg(this.subDomain);
            msg.setName("notifyFastReport");
            msg.put("logicalId", Long.valueOf(this.deviceId));
            msg.put("interval", Integer.valueOf(this.interval));
            msg.put("timespan", Integer.valueOf((int) ACDeviceDataManager.FAST_REPORT_DURATION));
            new ACServiceClient("zc-device-service", 1, ACConfiguration.getRouterAddr(), msg, this.callbackDelegate, new ACMsgListener() {
                /* class com.accloud.cloudservice.ACDeviceDataManager.NotifyFastReportTask.AnonymousClass2 */

                @Override // com.accloud.cloudservice.ACMsgListener
                public void finish(ACMsg resp) {
                    NotifyFastReportTask.this.callbackDelegate.success();
                    NotifyFastReportTask.this.callback = null;
                    if (ACDeviceDataManager.this.mHandler != null) {
                        ACDeviceDataManager.this.mHandler.postDelayed(NotifyFastReportTask.this, TimeUnit.SECONDS.toMillis(60));
                    }
                }
            }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Void[0]);
        }

        @Override // java.lang.Object
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            NotifyFastReportTask that = (NotifyFastReportTask) o;
            if (this.deviceId == that.deviceId) {
                return this.subDomain.equals(that.subDomain);
            }
            return false;
        }

        @Override // java.lang.Object
        public int hashCode() {
            return (this.subDomain.hashCode() * 31) + ((int) (this.deviceId ^ (this.deviceId >>> 32)));
        }
    }
}
